package com.example.administrator.schoolblog.Adpater;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.schoolblog.Beans.MainUserContent;
import com.example.administrator.schoolblog.DetailsActivity;
import com.example.administrator.schoolblog.MainActivity;
import com.example.administrator.schoolblog.R;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/5/18.
 */

public class MainUserContentAdapter extends RecyclerView.Adapter<MainUserContentAdapter.ViewHolder> {
    private Context mContext;
    private List<MainUserContent> mMainUserContent;
    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        CircleImageView userIcon;
        TextView nickname;
        TextView title;
        ImageView contentImage;
        public ViewHolder (View view){
            super(view);
            cardView = (CardView) view;
            nickname = (TextView) view.findViewById(R.id.blog_userContent_user_name);
            userIcon = (CircleImageView) view.findViewById(R.id.blog_userContent_user_icon);
            title = (TextView) view.findViewById(R.id.blog_userContent_title);
            contentImage = (ImageView) view.findViewById(R.id.blog_userContent_image);
        }
    }
    public  MainUserContentAdapter(List<MainUserContent> MainUserContentList){
        mMainUserContent = MainUserContentList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mContext==null){
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.blog_main_user_content_cardview,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                MainUserContent mainUserContent = mMainUserContent.get(position);
                Intent intent = new Intent(mContext, DetailsActivity.class);
                intent.putExtra("contentId",mainUserContent.getContentId());
                intent.putExtra("userId",mainUserContent.getUserId());
                mContext.startActivity(intent);
            }
        });
        return  holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MainUserContent mainUserContent = mMainUserContent.get(position);
        holder.nickname.setText(mainUserContent.getUserNickName());
        holder.title.setText(mainUserContent.getTitle());
        if (mainUserContent.getUserIconId()!=null){
            Glide.with(mContext).load(mainUserContent.getUserIconId()).into(holder.userIcon);
        }else {
            Glide.with(mContext).load(R.drawable.blog_user_icon).into(holder.userIcon);
        }
        if (mainUserContent.getContentImageId()!=null){
           // holder.contentImage.setMaxHeight(150);
            ViewGroup.LayoutParams params = holder.contentImage.getLayoutParams();
            params.height =500;
            holder.contentImage.setLayoutParams(params);
            Glide.with(mContext).load(mainUserContent.getContentImageId()).into(holder.contentImage);
        }
    }

    @Override
    public int getItemCount() {
        return mMainUserContent.size();
    }
}
